# 新建MySQL连接模块
from flask_sqlalchemy import SQLAlchemy

from config import MYSQL_CONFIG


class MySQLClient:
    def __init__(self, app=None):
        self.db = SQLAlchemy()
        if app:
            self.init_app(app)

    def init_app(self, app):
        app.config['SQLALCHEMY_DATABASE_URI'] = (
            f"mysql+pymysql://{MYSQL_CONFIG['user']}:{MYSQL_CONFIG['password']}@"
            f"{MYSQL_CONFIG['host']}:{MYSQL_CONFIG['port']}/"
            f"{MYSQL_CONFIG['database']}"
        )
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        self.db.init_app(app)

    def get_db(self):
        return self.db

db_client = MySQLClient()
